package manage;

import entity.HotelInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class HotelDBManager {

    public static ArrayList<HotelInfo> getAllHotels() {
        return getHotelsByCity("*");
    }

    public static ArrayList<HotelInfo> getHotelsByCity(String cityName) {
        ResultSet rs;
        ArrayList<HotelInfo> ret = new ArrayList<HotelInfo>();
        try {
            DBManager dbManager = new DBManager();
            if (cityName.equals("All Cities")) {
                rs = dbManager.queryDB("SELECT * FROM HOTEL_T");
            } else {
                rs = dbManager.queryDB("SELECT * FROM HOTEL_T WHERE CITY = '" + cityName + "'");
            }
            while (rs.next()) {
                String name = rs.getString("NAME");
                String imgUrl = "res/HotelImage/" + rs.getString("IMGURL") + ".jpg";
                String address = rs.getString("ADDRESS");
                String city = rs.getString("CITY");
                int price = rs.getInt("PRICE");
                String description = rs.getString("DESCRIPTION");
                int rating = rs.getInt("RATING");
                int room = rs.getInt("ROOM");
                ret.add(new HotelInfo(name, imgUrl, address, city, price, description, rating, room));
            }
            rs.close();
            dbManager.closeConnections();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ret;
    }

    public static void updateHotelTable(HotelInfo hotelInfo, int num) {
        String sql = "";
        if (num >= 0) {
            sql = "UPDATE HOTEL_T SET ROOM = ROOM + " + num + " WHERE NAME = '" + hotelInfo.getName() + "'";
        } else {
            sql = "UPDATE HOTEL_T SET ROOM = ROOM " + num + " WHERE NAME = '" + hotelInfo.getName() + "'";
        }
        DBManager dbManager = new DBManager();
        dbManager.establishConnection();
        dbManager.updateDB(sql);
        dbManager.closeConnections();
    }
}